package com.aoxon.cargo.component;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.aoxon.cargo.bean.GsonBean;
import com.aoxon.cargo.service.SupGetClassifyListService;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.CheckStateUtil;
import com.aoxon.cargo.util.EnvironmentUtil;
import com.aoxon.cargo.util.ExecutorUtil;
import com.aoxon.cargo.util.ToastUtil;
import com.google.gson.Gson;

/**
 * 显示分类的组件
 * 
 * @author ZZJ
 * 
 */
public class ClassifyDialog {

	private int length = 20;
	private int startIndex_1 = 0;
	private int startIndex_2 = 0;
	public static String[] strClassOnes;
	public static String[] strClassTwos;
	public boolean isLoading = false;
	private Gson gson = new Gson();

	private TextView tvClassOne;
	private TextView tvClassTwo;
	private MyListDialog myListDialog;
	private Activity activity;
	private Handler handler;

	public ClassifyDialog(Activity activity, Handler handler,
			TextView tvClassOne, TextView tvClassTwo) {
		this.activity = activity;
		this.handler = handler;
		this.tvClassOne = tvClassOne;
		this.tvClassTwo = tvClassTwo;
		myListDialog = new MyListDialog(activity);

		loadClassOneData(false);
		if (tvClassOne.getText().toString() != null
				&& !tvClassOne.getText().toString().equals("")) {
			loadClassTwoData(tvClassOne.getText().toString(), false);
		}

	}

	/**
	 * 显示分类一列表
	 */
	public void showClassOne() {
		if (strClassOnes != null) {
			myListDialog.show("选择类别一", strClassOnes, chooseClassOneCallBack);
		} else {
			loadClassOneData(true);
		}
	}

	/**
	 * 显示分类二列表
	 * 
	 * @param show
	 */
	public void showClassTwo() {
		if (tvClassOne.getText().toString() != null
				&& !tvClassOne.getText().toString().equals("")) {
			if (strClassTwos != null) {
				myListDialog.show("选择类别二", strClassTwos, tvClassTwo);
			} else {
				if (!isLoading) {
					ToastUtil.show(activity.getBaseContext(), "无相应数据");
				}
			}
		} else {
			ToastUtil.show(activity.getBaseContext(), "请先选择分类一");
			showClassOne();
		}
	}

	/**
	 * 下载分类一列表数据
	 */
	public void loadClassOneData(final boolean show) {
		if (!isLoading) {
			ExecutorUtil.getCachedExecutorService().submit(new Runnable() {
				@Override
				public void run() {
					Message msg = new Message();
					if (EnvironmentUtil.checkNetState(activity.getBaseContext())) {
						isLoading = true;
						try {
							GsonBean gsonBean = new SupGetClassifyListService()
									.getGoodsClassifyOne(startIndex_1, length);
							if (CheckStateUtil.check(gsonBean)) {
								strClassOnes = gson.fromJson(
										gsonBean.getStrJson(), String[].class);
								startIndex_1 += strClassOnes.length;
								if (show) {
									msg.what = MyStateUtil.GET_CLASSONE_RETURN;
								}
							} else {
								if (show) {
									msg.what = MyStateUtil.RETURN_FAIL;
								}

							}
						} catch (Exception e) {
							e.printStackTrace();
							msg.what = MyStateUtil.ERROR_NET_CONNECTION;
						}
					} else {
						msg.what = MyStateUtil.NOT_NET_CONNECTION;
					}
					handler.sendMessage(msg);
					isLoading = false;
				}
			});
		}
	}

	/**
	 * 下载分类二列表数据
	 */
	public void loadClassTwoData(final String strClassifyOne, final boolean show) {
		if (!isLoading) {
			ExecutorUtil.getCachedExecutorService().submit(new Runnable() {
				@Override
				public void run() {
					Message msg = new Message();
					if (EnvironmentUtil.checkNetState(activity.getBaseContext())) {
						isLoading = true;
						try {
							GsonBean gsonBean = new SupGetClassifyListService()
									.getGoodsClassifyTwo(startIndex_2,length,strClassifyOne);
							if (CheckStateUtil.check(gsonBean)) {
								strClassTwos = gson.fromJson(
										gsonBean.getStrJson(), String[].class);
								startIndex_2 += strClassTwos.length;
								if (show) {
									msg.what = MyStateUtil.GET_CLASSTWO_RETURN;
								}
							} else {
								if (show) {
									msg.what = MyStateUtil.RETURN_FAIL;
								}
							}
						} catch (Exception e) {
							e.printStackTrace();
							msg.what = MyStateUtil.ERROR_NET_CONNECTION;
						}
					} else {
						msg.what = MyStateUtil.NOT_NET_CONNECTION;
					}
					handler.sendMessage(msg);
					isLoading = false;
				}
			});
		}
	}

	/**
	 * 选择分类一后立即加载分类二数据的回调
	 */
	MyCallBack chooseClassOneCallBack = new MyCallBack() {
		@Override
		public void callback(String choose) {
			// TODO Auto-generated method stub
			if (!tvClassOne.getText().toString().equals(choose)) {
				tvClassOne.setText(choose);
				tvClassTwo.setText("");
				strClassTwos = null;
				startIndex_2 = 0;
				loadClassTwoData(choose, true);
			}
		}
	};
}
